package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import core.InvalidBEncodingException;

public class FenetreAddUrl extends JFrame {

    private JTextField url;
    private JLabel text;
    private JPanel conteneur;
    private JButton ok;
    private FenetrePrincipale fenetrePrincipale;

    public FenetreAddUrl(final FenetrePrincipale fenetrePrincipale) {
        this.fenetrePrincipale = fenetrePrincipale;

        this.text = new JLabel();
        this.url = new JTextField();
        this.conteneur = new JPanel();
        this.ok = new JButton("OK");


        this.ok.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    String urlField = FenetreAddUrl.this.url.getText();

                    FenetreAddUrl.this.modifyLabel("Téléchargement du fichier en cours...");

                    if (!urlField.equals("") && urlField.matches("^(http|ftp)://.*\\.torrent/?")) {
                        URL url = new URL(urlField);
                        ReadableByteChannel rbc = Channels.newChannel(url.openStream());

                        String path = "/home/dimitri/file.torrent";
                        FileOutputStream fos = new FileOutputStream(path);
                        fos.getChannel().transferFrom(rbc, 0, 1 << 24);
                        System.out.println("Fichier dl !");
                        try {
                            FenetreAddUrl.this.fenetrePrincipale.addTorrent(path);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(FenetreAddUrl.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (ExecutionException ex) {
                            Logger.getLogger(FenetreAddUrl.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                } catch (IOException e1) {
                    new MessageErreur("Impossible de récupérer le fichier distant.");
                } catch (InvalidBEncodingException e1) {
                    new MessageErreur("Impossible de lire le fichier.");
                }

                fenetrePrincipale.getAddUrl().setEnabled(true);
                FenetreAddUrl.this.dispose();

            }
        });

        this.url.setPreferredSize(new Dimension(190, 20));

        this.conteneur.add(this.text, BorderLayout.NORTH);
        this.conteneur.add(this.url);
        this.conteneur.add(this.ok, BorderLayout.SOUTH);

        this.add(this.conteneur);
        this.setTitle("Ouvrir via URL");
        this.modifyLabel("Entrer l'url de votre fichier torrent");
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setVisible(true);
    }

    public void modifyLabel(String content) {
        this.text.setText(content);
        this.text.repaint();
        this.setSize(content.length() * 8, 127);

        this.repaint();
    }
}
